package com.iceengine.j3d.models;

/**
 * Wavefront modelloader class.
 * Currently Supports:
 * -Every type of vertices
 * -Triangle faces
 * -Comments
 * @author penguin
 */
import com.iceengine.j3d.objects.*;
import java.io.*;
import org.lwjgl.util.vector.*;
import java.util.ArrayList;
import com.iceengine.j3d.helper.Loader;
import org.newdawn.slick.opengl.Texture;

import java.util.StringTokenizer;
public class Wavefront {
    private SimpleObject ourObject;
    private ArrayList<Vertex> Vertices;
    private ArrayList<Vector2f> UV;
    private StringTokenizer stringTokens;

    /**
     * Initializes the modelloader.
     * @param Translation Translation for the model.
     */
    public Wavefront(Vector3f Translation)
    {
        ourObject = new SimpleObject(Translation);
        Vertices = new ArrayList<Vertex>();
        UV = new ArrayList<Vector2f>();
    }

    /**
     * Loads the model at the given path.
     * @param Path Path where the model you want to load is located.
     */
    public void loadModel(String Path)
    {
        try
        {
            BufferedReader theReader = new BufferedReader(new FileReader(Path));
            String currentLine = "";

            IFigure currentFigure = null;

            while((currentLine = theReader.readLine()) != null)
            {
                if(currentLine.length() > 0)
                {
                    if(currentLine.startsWith("v "))
                    {
                        stringTokens = new StringTokenizer(currentLine);
                        stringTokens.nextToken();
                        Vector3f theVertex = new Vector3f(Float.parseFloat(stringTokens.nextToken()),Float.parseFloat(stringTokens.nextToken()),Float.parseFloat(stringTokens.nextToken()));
                        Vertex tmp = new Vertex(theVertex, null);
                        Vertices.add(tmp);
                    } else if(currentLine.startsWith("vt "))
                    {
                        stringTokens = new StringTokenizer(currentLine);
                        stringTokens.nextToken();
                        Vector2f theVertex = new Vector2f(Float.parseFloat(stringTokens.nextToken()),Float.parseFloat(stringTokens.nextToken()));
                        UV.add(theVertex);
                    } else if(currentLine.startsWith("vn "))
                    {
                        stringTokens = new StringTokenizer(currentLine);
                        stringTokens.nextToken();
                        Vector3f theVertex = new Vector3f(Float.parseFloat(stringTokens.nextToken()),Float.parseFloat(stringTokens.nextToken()),Float.parseFloat(stringTokens.nextToken()));
                        Vertex tmp = new Vertex(theVertex, null);
                        Vertices.add(tmp);
                    } else if(currentLine.startsWith("f "))
                    {
                        stringTokens = new StringTokenizer(currentLine);
                        stringTokens.nextToken();
                        int[] vertices = new int[3];
                        vertices[0] = Integer.parseInt(stringTokens.nextToken());
                        vertices[1] = Integer.parseInt(stringTokens.nextToken());
                        vertices[2] = Integer.parseInt(stringTokens.nextToken());
                        Vertex[] usedVertices = new Vertex[3];
                        usedVertices[0] = Vertices.get(vertices[0]-1);
                        usedVertices[1] = Vertices.get(vertices[1]-1);
                        usedVertices[2] = Vertices.get(vertices[2]-1);
                        currentFigure = new Triangle(usedVertices[0], usedVertices[1], usedVertices[2], null, null);
                        ourObject.addFigure(currentFigure);
                    }
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    /**
     * Gets the generated object out of the wavefront file.
     * @return The object.
     */
    public SimpleObject getObject()
    {
        return ourObject;
    }
}
